﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{    
    public partial class JLTGB12LSData : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        /*public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        SETINTEXDB INTEXDB = new SETINTEXDB();
         */
        public JLTGB12LSData()
            : base()
        {
        }
         public JLTGB12LSData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetGB12LS(DataRow dwTABEL)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "GB12LS";
            string ART = dwTABEL["ART"].ToString().Trim();
            try
            {
                orlReadCommand = INTEXDBC.OracleDB.CreateCommand();
                orlReadCommand.CommandText = "  SELECT TRIM(A.ZUART) AS ART,TRIM(B.ABKKDAUFTNR) AS CO,A.ZUSPPART, "
                    + "  A.ZUAUFKRS||'-'||TO_NUMBER(A.ZUAUFLFD)||'-'||TO_NUMBER(A.ZUAUFPOS) AS ORD, "
                    + "  SUM((A.ZUEINTKG - A.ZUENTNKG)*0.001) AS QTY "
                    + "  FROM ZUTEI A,ABKOPF B,GLLG C "
                    + "  WHERE A.ZUERLKZ = ' ' AND A.ZUFLAG = 'S' "
                    + "  AND A.ZUEINTKG > A.ZUENTNKG "
                    + "  AND A.ZUMANDANT = B.ABKMANDANT AND A.ZUAUFKRS = B.ABKAUFKRS "
                    + "  AND A.ZUAUFLFD = B.ABKAUFLFD  "
                    + "  AND A.ZUMANDANT = C.GLMANDANT AND A.ZUPARTIE = C.GLPARTIE "
                    + "  AND A.ZUPARTIEKZ = C.GLPARTIEKZ AND A.ZULBEHI = C.GLLBEHI "
                    + "  AND C.GLLO ='01' AND C.GLLB = 'PS' "
                    + "  AND TRIM(A.ZUART) LIKE '"+ART+"%' "
                    + "  GROUP BY A.ZUART,B.ABKKDAUFTNR,A.ZUSPPART, "
                    + "  A.ZUAUFKRS,A.ZUAUFLFD,A.ZUAUFPOS "
                    + "  ORDER BY 1,3 "
                    ;

                Result = INTEXDBC.Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    byte[] b;
                    string value;
                    foreach (DataRow dw in Result.Tables[TableName].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["CO"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["CO"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["ZUSPPART"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["ZUSPPART"] = value;

                    }
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        
        public DataSet BuildData(DataRow dwUI)
        {
            /*DataSet SResult = new DataSet();
            SETINTEXDB SReturn = new SETINTEXDB(); */
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                setResult = GetGB12LS(dwUI);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }

    }
}